package com.zhuozhou.jdyc.main.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityClassifyBinding;
import com.zhuozhou.jdyc.main.goods.activity.GoodsDetailActivity;
import com.zhuozhou.jdyc.main.main.adapter.BarterShopAdapter;
import com.zhuozhou.jdyc.main.main.adapter.ClassifyAdapter;
import com.zhuozhou.jdyc.main.main.adapter.IndexShopAdapter;
import com.zhuozhou.jdyc.main.main.adapter.IntegralShopAdapter;
import com.zhuozhou.jdyc.main.main.adapter.ShopClassifyAdapter;
import com.zhuozhou.jdyc.main.main.bean.BasicsCommodityTypeBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsTypeListBean;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/8/17
 * Describe:分类
 */
public class GoodsClassifyActivity extends BaseActivity<MainViewModel, ActivityClassifyBinding> implements ClassifyAdapter.OnTabItemClickListener,
        IndexShopAdapter.OnShopItemClickListener, ShopClassifyAdapter.OnItemClickListener, BarterShopAdapter.OnShopItemClickListener, IntegralShopAdapter.OnShopItemClickListener {

    private ClassifyAdapter classifyAdapter;
    private IndexShopAdapter indexShopAdapter;
    private BarterShopAdapter barterShopAdapter;
    private IntegralShopAdapter integralShopAdapter;
    private ShopClassifyAdapter shopClassifyAdapter;
    private List<BasicsCommodityTypeBean.ArrayBean> listShop = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_classify;
    }

    @Override
    public String setTitleBar() {
        return "商品分类";
    }

    @Override
    public void initView() {
        if (baseBundle.getInt("type", 1) == 1 || baseBundle.getInt("type", 1) == 3) {
            setThemeBarAndToolBarColor(R.color.themeColor);
            binding.llColor.setBackgroundColor(getResources().getColor(R.color.themeColor));
        } else {
            setThemeBarAndToolBarColor(R.color.theme_green);
            binding.llColor.setBackgroundColor(getResources().getColor(R.color.theme_green));
        }
        binding.mRecyclerViewMerchant.setPadding(10, 10, 10, 0);
        mViewModel.BasicsTypeListMutableLiveData.observe(this, v -> {
            List<TabItemBean> listItem = new ArrayList<>();
            listItem.add(new TabItemBean(-1, "商品分类", true));
            for (BasicsTypeListBean.ArrayBean.TypeListBean item : v.getArray().getTypeList()) {
                listItem.add(new TabItemBean(item.getId(), item.getName(), false));
            }
            //分类
            classifyAdapter = new ClassifyAdapter(this);
            classifyAdapter.setList(listItem);
            classifyAdapter.setOnTabItemClickListener(this);
            binding.mRecyclerViewClassify.setAdapter(classifyAdapter);
            binding.mRecyclerViewClassify.setLayoutManager(new MyLayoutManager(this));
            listShop = v.getArray().getCommodityList();
            if (StringUtil.isEmpty(baseBundle.getInt("id"))) {
                //全部商品
                setShopList(v.getArray().getCommodityList());
            } else {
                setClassifyAdapter(baseBundle.getInt("id"));
            }
        });
        //分类的分类
        mViewModel.BasicsTypeDetailsMutableLiveData.observe(this, v -> {
            shopClassifyAdapter = new ShopClassifyAdapter(this);
            shopClassifyAdapter.setList(v.getArray());
            shopClassifyAdapter.setOnItemClickListener(this);
            binding.mRecyclerViewMerchant.setAdapter(shopClassifyAdapter);
            binding.mRecyclerViewMerchant.setLayoutManager(new FullyGridLayoutManager(this, 3));
        });
        //分类商品
        mViewModel.BasicsTypeDetails2MutableLiveData.observe(this, v -> {
            setShopList(v.getArray());
        });
        //搜索结果
        mViewModel.basicsSearchMutableLiveData.observe(this, v -> {
            setShopList(v.getObj());
        });
        binding.searchEdit.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.searchEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                try {
                    mViewModel.getBasicsSearch(UserAccountHelper.getNewCity(), v.getText().toString(), baseBundle.getInt("type"), 0);
                    setClassifyAdapter(-1);
                    // 当按了搜索之后关闭软键盘
                    ((InputMethodManager) binding.searchEdit.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        });
        binding.tvSearch.setOnClickListener(v -> {
            mViewModel.getBasicsSearch(UserAccountHelper.getNewCity(), binding.searchEdit.getText().toString(), baseBundle.getInt("type"), 0);
            setClassifyAdapter(-1);
        });
    }

    public void setClassifyAdapter(int id) {
        List<TabItemBean> list = new ArrayList<>();
        for (TabItemBean item : classifyAdapter.getList()) {
            if (item.getIsView()) {
                item.setIsView(false);
            }
            if (item.getId() == id) {
                item.setIsView(true);
            }
            list.add(item);
        }
        classifyAdapter.setList(list);
        classifyAdapter.notifyDataSetChanged();
    }

    public void setShopList(List<BasicsCommodityTypeBean.ArrayBean> list) {
        switch (baseBundle.getInt("type")) {
            case 1:
                indexShopAdapter = new IndexShopAdapter(this);
                indexShopAdapter.setList(list);
                indexShopAdapter.setIsClassify(true);
                indexShopAdapter.setOnShopItemClickListener(this);
                binding.mRecyclerViewMerchant.setAdapter(indexShopAdapter);
                break;
            case 2:
                integralShopAdapter = new IntegralShopAdapter(this);
                integralShopAdapter.setList(list);
                integralShopAdapter.setIsClassify(true);
                integralShopAdapter.setOnShopItemClickListener(this);
                binding.mRecyclerViewMerchant.setAdapter(integralShopAdapter);
                break;
            case 3:
                barterShopAdapter = new BarterShopAdapter(this);
                barterShopAdapter.setList(list);
                barterShopAdapter.setIsClassify(true);
                barterShopAdapter.setOnShopItemClickListener(this);
                binding.mRecyclerViewMerchant.setAdapter(barterShopAdapter);
                break;

        }
        binding.mRecyclerViewMerchant.setLayoutManager(new FullyGridLayoutManager(this, 2));
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getBasicsTypeList(UserAccountHelper.getNewCity(), bundle.getInt("type"));
        if (StringUtil.isNotEmpty(bundle.getInt("id")))
            mViewModel.getBasicsTypeDetails(UserAccountHelper.getNewCity(), bundle.getInt("type"), bundle.getInt("id"));
        if (StringUtil.isNotEmpty(bundle.getString("searchEdit"))) {
            binding.searchEdit.setText(bundle.getString("searchEdit"));
            mViewModel.getBasicsSearch(UserAccountHelper.getNewCity(), bundle.getString("searchEdit"), baseBundle.getInt("type"), 0);
        }
    }

    @Override
    public void onTabItemClick(View view, int position) {
        if (StringUtil.isEmpty(binding.searchEdit.getText().toString())) {
            if (classifyAdapter.getList().get(position).getId() == -1) {
                setShopList(listShop);
            } else {
                mViewModel.getBasicsTypeDetails(UserAccountHelper.getNewCity(), baseBundle.getInt("type"), baseBundle.getInt("id"));
            }
        } else {
            mViewModel.getBasicsSearch(UserAccountHelper.getNewCity(), binding.searchEdit.getText().toString(), baseBundle.getInt("type"), classifyAdapter.getList().get(position).getId());
        }
    }

    @Override
    public void onShopItemClick(View view, int position) {
        Intent intent = new Intent(this, GoodsDetailActivity.class);
        Bundle bundle = new Bundle();
        switch (baseBundle.getInt("type")) {
            case 1:
                bundle.putString("id", indexShopAdapter.getList().get(position).getCommodity_id() + "");
                break;
            case 2:
                bundle.putString("id", integralShopAdapter.getList().get(position).getCommodity_id() + "");
                break;
            case 3:
                bundle.putString("id", barterShopAdapter.getList().get(position).getCommodity_id() + "");
                break;

        }
        bundle.putInt("type", baseBundle.getInt("type"));
        intent.putExtras(bundle);
        startActivity(intent);

    }

    @Override
    public void onItemClick(View view, int position) {
        mViewModel.getBasicsTypeDetails2(UserAccountHelper.getNewCity(), 1, shopClassifyAdapter.getList().get(position).getBid());
    }
}
